﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ClassLibrary
{
    public class BigendianWriter
    {
        public BigendianWriter(Stream stream)
        {
            this.encoder = Encoding.GetEncoding("GBK");
            this.stream = stream;
        }

        private Encoding encoder;

        public Stream stream
        {
            get;
            private set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="length">OctetString的占用字节数</param>
        /// <returns></returns>
        public void WriteOctetString(string value, int length)
        {
            byte[] valueBytes = encoder.GetBytes(value??"");
            if (valueBytes.Length <= length)
            {
                stream.Write(valueBytes, 0, valueBytes.Length);
                //位数不足，右补16进制0x00,汉字采用GBK编码
                for (int i = 0; i < length - valueBytes.Length; i++)
                {
                    stream.WriteByte(0x00);
                } 
            }
            else
            {
                throw new Exception("Encoded bytes should not be larger than the length");
            }
        }

        public void WriteByte7DateTime(DateTime value)
        {
            stream.WriteByte((byte)value.Day);
            stream.WriteByte((byte)value.Month);
            stream.Write(BytesConverter.ToWord(value.Year), 0, 2);
            stream.WriteByte((byte)value.Hour);
            stream.WriteByte((byte)value.Minute); 
            stream.WriteByte((byte)value.Second);
        }

        public void WriteUInt16(UInt16 value)
        {
            byte[] valueBytes = BitConverter.GetBytes(value);
            Array.Reverse(valueBytes);
            stream.Write(valueBytes, 0, 2);
        }

        public void WriteUInt32(UInt32 value)
        {
            byte[] valueBytes = BitConverter.GetBytes(value);
            Array.Reverse(valueBytes);
            stream.Write(valueBytes, 0, 4);
        }

        public void WriteByte(byte value)
        {
            stream.WriteByte(value);
        }

        public void WriteBytes(byte[] value)
        {
            stream.Write(value, 0, value.Length);
        }

        public void WriteTime_t(DateTime value)
        {
            byte[] valueBytes = BitConverter.GetBytes(value.Ticks);
            Array.Reverse(valueBytes, 0, 8);
            stream.Write(valueBytes, 0, 8);
        }

        /// <summary>
        /// 809协议UTC时间
        /// 如2010-1-10 9:7:54的UTC值为1263085674，其在协议中表示为0x000000004B49286A.
        /// </summary>
        /// <param name="value"></param>
        public void WriteUtcTime(DateTime value)
        {
            DateTime dt = value.ToUniversalTime();
            DateTime utc = DateTime.Parse("1970-1-1 0:0:00");

            long diffseconds = Convert.ToInt64(((System.TimeSpan)(dt - utc)).TotalSeconds);
            byte[] valueBytes = BitConverter.GetBytes(diffseconds);
            Array.Reverse(valueBytes, 0, 8);
            stream.Write(valueBytes, 0, 8);
        }

    }
}
